using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace Betarium.PotonEditor
{
	public partial class FormatForm : Form
	{
		protected ImageFormat m_nFileFormat = ImageFormat.Bmp;
		protected PixelFormat m_nPixcelFormat = PixelFormat.Format24bppRgb;
		protected Image m_targetImage;
		protected int m_nQuality = 100;

		public ImageFormat FileFormat
		{
			get { return m_nFileFormat; }
			set { m_nFileFormat = value; }
		}
		public PixelFormat DataFormat
		{
			get { return m_nPixcelFormat; }
			set { m_nPixcelFormat = value; }
		}
		public int Quality
		{
			get { return m_nQuality; }
			set { m_nQuality = value; }
		}

		public Image TargetImage
		{
			get { return m_targetImage; }
			set { m_targetImage = value; }
		}

		public FormatForm()
		{
			InitializeComponent();
		}

		private void m_ctrlOK_Click(object sender, EventArgs e)
		{
			if (m_ctrlFormatBmp.Checked)
			{
				m_nFileFormat = ImageFormat.Bmp;
				m_nPixcelFormat = PixelFormat.Format24bppRgb;
			}
			else if (m_ctrlFormatPng.Checked)
			{
				m_nFileFormat = ImageFormat.Png;
				if (m_ctrlAlphaEnable.Checked)
				{
					m_nPixcelFormat = PixelFormat.Format32bppArgb;
				}
				else
				{
					m_nPixcelFormat = PixelFormat.Format24bppRgb;
				}
			}
			else if (m_ctrlFormatJpg.Checked)
			{
				m_nQuality = Int32.Parse(m_ctrlQuality.Text);
				m_nFileFormat = ImageFormat.Jpeg;
				m_nPixcelFormat = PixelFormat.Format24bppRgb;
			}
		}

		private void FormatForm_Load(object sender, EventArgs e)
		{
			int nDepth = 0;
			//m_nPixcelFormat = m_targetImage.PixelFormat;
			switch (m_targetImage.PixelFormat)
			{
				case PixelFormat.Format8bppIndexed:
					nDepth = 8;
					break;
				case PixelFormat.Format16bppRgb555:
				case PixelFormat.Format16bppRgb565:
					nDepth = 16;
					break;
				case PixelFormat.Format24bppRgb:
					nDepth = 24;
					break;
				case PixelFormat.Format32bppArgb:
				case PixelFormat.Format32bppRgb:
					nDepth = 32;
					break;
			}
			if (m_nFileFormat == ImageFormat.Bmp)
			{
				m_ctrlOldFormat.Text = String.Format("Bitmap ColorBits={0}", nDepth);
				m_ctrlFormatBmp.Checked = true;
			}
			else if (m_nFileFormat == ImageFormat.Png)
			{
				m_ctrlOldFormat.Text = String.Format("PNG ColorBits={0}", nDepth);
				m_ctrlFormatPng.Checked = true;
			}
			else if (m_nFileFormat == ImageFormat.Jpeg)
			{
				m_ctrlOldFormat.Text = String.Format("Jpeg ColorBits={0}", nDepth);
				m_ctrlFormatJpg.Checked = true;
			}
			else
			{
				m_ctrlFormatBmp.Checked = true;
			}
			m_ctrlQuality.Text = "" + m_nQuality;
			UpdateControl();
		}

		void UpdateControl()
		{
			m_ctrlAlphaEnable.Enabled = m_ctrlFormatPng.Checked;
			m_ctrlAlphaEnable.Checked = (m_ctrlFormatPng.Checked && m_targetImage.PixelFormat == PixelFormat.Format32bppArgb);
			m_ctrlQuality.Enabled = m_ctrlFormatJpg.Checked;
		}

		private void m_ctrlFormatBmp_CheckedChanged(object sender, EventArgs e)
		{
			UpdateControl();
		}

		private void m_ctrlFormatPng_CheckedChanged(object sender, EventArgs e)
		{
			UpdateControl();
		}

		private void m_ctrlFormatJpg_CheckedChanged(object sender, EventArgs e)
		{
			UpdateControl();
		}

	}
}